<?php

namespace app\common\service;

use think\facade\Db;

class DatabaseService
{
    /*
     * 根据url获取库
     */
    public static function getDatabaseNameByUrl(){
        $url = $_SERVER['REDIRECT_URL'];
        if (!stripos($url, 'getAreaData')) {
            //API
            /*if (stripos($url, 'api') && input('location_id')) {
                //小程序API
                $locationId = input('location_id');
                foreach (config('areaCache') as $k => $v) {
                    if ($locationId == $v['id']){
                        //县市
                        $databaseName = $v['db'];
                    }else{
                        //乡镇
                        foreach ($v['child'] as $k1 => $v1) {
                            if ($v1['id'] == $locationId) $databaseName = $v['db'];
                        }
                    }
                }
            }*/

            //else {
            //管理后台
                $domain = $_SERVER['SERVER_NAME'].'.';
                $domain='szq.jq.polite.';//写死肃州区，然后通过账号密码区分果园等乡镇
                //$domain='super.jq.polite.';//超级管理员登入时切换

                if ($domain == 'super.jq.polite.') {
                    $databaseName = 'polite-common';
                } else {
                    foreach (config('areaCache') as $k => $v) {
                        foreach ($v['child'] as $k1=>$v1){
                            if ($v1['domain'] == $domain) {
                                $databaseName = $v1['db'];
                                break;
                            }
                        }
                    }
                }
                if (empty($databaseName)) {
                    echo '无法访问！';
                    exit;
                }
            //}
        } else {
            $databaseName = 'polite-common';
        }
        return $databaseName;
    }

}